package xyz.yeidi.dao.impl;

import xyz.yeidi.dao.api.OrdersDao;
import xyz.yeidi.pojo.Orders;
import xyz.yeidi.utils.ConnUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author JIP
 * @Date 2021-08-07 15:21
 * @Version 1.0
 * @Describe :
 */
public class OrdersDaoImpl implements OrdersDao {
    @Override
    public boolean addOrders(Orders orders) throws Exception {
        String sql = "insert into orders(userId,businessId, orderDate,orderTotal,daId)" +
                "values (?,?,?,?,?)";
        Connection conn = ConnUtils.getConnection();
        boolean flag = true;
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,orders.getUserId());
        ps.setObject(2,orders.getBusinessId());
        ps.setObject(3,orders.getOrderDate());
        ps.setObject(4,orders.getOrderTotal());
        ps.setObject(5,orders.getDaId());
        if(ps.executeUpdate()==0){
            flag = false;
        }
        ConnUtils.close(ps);
        return flag;
    }

    @Override
    public Orders getOrdersById(Integer id) throws Exception {
        String sql = "select userId,businessId,orderDate,orderTotal,daId from orders where orderId = ?";
        Connection conn = ConnUtils.getConnection();
        Orders orders = null;
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,id);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            orders = new Orders();
            orders.setUserId(rs.getInt(1));
            orders.setBusinessId(rs.getInt(2));
            orders.setOrderDate(rs.getString(3));
            orders.setOrderTotal(rs.getDouble(4));
            orders.setDaId(rs.getInt(5));
        }
        ConnUtils.close(rs,ps);
        return orders;
    }

    @Override
    public boolean deleteOrdersById(Integer id) throws Exception {
        String sql = "delete from orders where orderId = ?";
        Connection conn = ConnUtils.getConnection();
        boolean flag = true;
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,id);
        if(ps.executeUpdate()==0){
            flag = false;
        }
        ConnUtils.close(ps);
        return flag;
    }
    @Override
    public List<Orders> getOrdersByUserId(Integer userId) throws Exception {
        String sql = "select orderId,businessId,orderDate,orderTotal,daId from orders where userId = ?";
        Connection conn = ConnUtils.getConnection();
        List<Orders> list = new ArrayList<>();
        Orders orders = null;

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,userId);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            orders = new Orders();
            orders.setUserId(userId);
            orders.setOrderId(rs.getInt(1));
            orders.setBusinessId(rs.getInt(2));
            orders.setOrderDate(rs.getString(3));
            orders.setOrderTotal(rs.getDouble(4));
            orders.setDaId(rs.getInt(5));
            list.add(orders);
        }
        ConnUtils.close(rs,ps);
        return list;
    }

    @Override
    public List<Orders> getOrdersByBusinessId(Integer businessId) throws Exception {
        String sql = "select userId,orderId,orderDate,orderTotal,daId from orders where businessId = ?";
        Connection conn = ConnUtils.getConnection();
        List<Orders> list = new ArrayList<>();
        Orders orders = null;

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,businessId);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            orders = new Orders();
            orders.setBusinessId(businessId);
            orders.setUserId(rs.getInt(1));
            orders.setOrderId(rs.getInt(2));
            orders.setOrderDate(rs.getString(3));
            orders.setOrderTotal(rs.getDouble(4));
            orders.setDaId(rs.getInt(5));
            list.add(orders);
        }
        ConnUtils.close(rs,ps);
        return list;
    }

    @Override
    public Integer getOrderIdByUserId(Integer userId) throws Exception {
        String sql = "select orderId from orders where userId = ? group by orderId desc limit 1";
        Connection conn = ConnUtils.getConnection();
        Integer result = null;
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,userId);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            result = rs.getInt(1);
        }
        ConnUtils.close(rs,ps);
        return result;
    }
}
